package com.lpc.broadcastreceivertest;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.content.PermissionChecker;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;


/**
 * localBroadcastManager发送广播
 */
public class SecondActivity extends AppCompatActivity {

    public static final String ACTION = "com.lpc.broadcastreceivertest.MyReceiver";
    private LocalBroadcastManager localBroadcastManager;
    private MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        init();
    }

    private void init() {

        IntentFilter filter = new IntentFilter(ACTION);
        myReceiver = new MyReceiver();

        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localBroadcastManager.registerReceiver(myReceiver, filter);
    }

    public void click2(View view) {

        Intent i = new Intent();
        i.setAction(ACTION);
        localBroadcastManager.sendBroadcast(i);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        localBroadcastManager.unregisterReceiver(myReceiver);
    }

}
